package com.employeemanagementsystem.util;

import java.sql.*;

public class DBUtil {
    private static final String URL = "jdbc:mysql://localhost:3306/employee_management_system";
    private static final String USER = "root";
    private static final String PASSWORD = "1234";
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";

    public static Connection getConnection(){
        String url1 = URL;
        String url2 = "?user="+USER+"&password="+PASSWORD;
        String url3 = "&useUnicode=true&characterEncoding=UTF-8";
        String url = url1+url2+url3;
        try{
            Class.forName(DRIVER);
            Connection connection = DriverManager.getConnection(url);
            return connection;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void close(Connection connection, PreparedStatement pstm, ResultSet resultSet){
        try{
            if(resultSet != null){
                resultSet.close();
            }
            if(pstm != null){
                pstm.close();
            }
            if(connection != null){
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
